package com.fangcun.controller.sys;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.bean.sys.pay.PayResultMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.canstant.PublicConstant;
import com.fangcun.common.enums.PayCodeEnum;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.XStreamUtils;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.date.DateUtils;
import com.fangcun.common.util.wxpay.WXPayUtils;
import com.fangcun.common.util.wxpay.WechatPayResponse;
import com.fangcun.entity.SysPayment;
import com.fangcun.mapper.SysPaymentMapper;
import com.fangcun.service.SystemService;
import com.fangcun.service.api.OrderApiService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月29日 - 下午3:00:13 @description：->
 */
@RestController
@RequestMapping("/sys/{version}/pay")
public class PayController {

	private static Logger LOG = LoggerFactory.getLogger(PayController.class);

	@Resource(name = "systemService")
	private SystemService systemService;
	@Resource(name = "orderApiService")
	private OrderApiService orderApiService;
	@Autowired
	private SysPaymentMapper paymentMapper;

	/** 支付列表获取 */
	@ApiVersion(1.0)
	@RequestMapping("list")
	public ResponseMsg list(String view) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(systemService.payList(view));
		} catch (ProException e) {
			LOG.error("-系统-支付列表获取出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-支付列表获取异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 微信支付回调接口，供微信调用 */
	@ApiVersion(1.0)
	@RequestMapping(value = "wx/notify", produces = { "application/xml;charset=UTF-8" })
	public Object wxNotify(@RequestBody(required = false) String param) {
		try {
			LOG.info("-支付-微信回调内容信息：" + param);
			if (param.contains("<req_info>")) {
				return "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>";
			}
			WechatPayResponse wechatPayResponse = XStreamUtils.xmlToBean(WechatPayResponse.class, param);
			if (wechatPayResponse != null && param.contains("return_code") && "SUCCESS".equals(wechatPayResponse.getReturn_code())) {
				SysPayment payment = paymentMapper.selectByAppid(wechatPayResponse.getAppid());
				if (payment == null) {
					throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), "支付信息不存在");
				}
				if (!WXPayUtils.isSignatureValid(param, payment.getPayAppKey())) {
					return "<xml><return_code>FAIL</return_code><return_msg>FAIL</return_msg></xml>";
				}

				PayResultMsg payResultMsg = new PayResultMsg();
				payResultMsg.setBillNo(wechatPayResponse.getTransaction_id());
				payResultMsg.setOrderNumber(wechatPayResponse.getOut_trade_no());
				payResultMsg.setPayTime(DateUtils.DF_CLOSED.get().parse(wechatPayResponse.getTime_end()));
				payResultMsg.setPayPrice(wechatPayResponse.getTotal_fee().divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP));
				payResultMsg.setPayType(PayCodeEnum.WECHAT.getType());
				payResultMsg.setPayTypeName(PayCodeEnum.WECHAT.getPayName());
				payResultMsg.setAppid(wechatPayResponse.getAppid());
				payResultMsg.setOpenid(wechatPayResponse.getOpenid());

				// 判断去哪里执行回调参数
				if (payResultMsg.getOrderNumber().indexOf(PublicConstant.ORDER_TYPE_1) == 0) {// 商品支付回调
					orderApiService.dealPayNotify(payResultMsg);
				}

				return "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>";
			}
		} catch (ProAffairException e) {
			LOG.error("-支付-微信支付回调->{}->param:{}", e.getErrorMsg(), param);
			if (ErrCode.NO_DATA.getReturnCode().equals(e.getErrorCode())) {// 找不到订单数据，直接退款
				// 退款操作
				return "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>";
			}
		} catch (Exception e) {
			LOG.error("-支付-微信支付回调异常->param:{}->异常：{}", param, PublicMethod.printErrorTrace(e));
		}
		return "<xml><return_code>FAIL</return_code><return_msg>FAIL</return_msg></xml>";
	}
}
